﻿#include "algo_cam_h_form.h"
#include "ui_algo_cam_h_form.h"
#include <QDebug>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include "singleton.h"
#include <QMessageBox>
bool Algo_Cam_H_Form::mode_beltwidth = false;
Algo_Cam_H_Form::Algo_Cam_H_Form(QString chnid, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Algo_Cam_H_Form)
{
    ui->setupUi(this);
    m_chnid = chnid;
    this->initForm();

}

Algo_Cam_H_Form::~Algo_Cam_H_Form()
{
    delete ui;
}

void Algo_Cam_H_Form::initForm()
{
    this->InputValidator();
    connect(ui->sb_set_beltwidth, &SwitchButton::checkedChanged, [this](bool checked)
    {
        this->mode_beltwidth = checked;
        if(checked)
            QMessageBox::information(nullptr, "提示", "已切换至皮带宽度设置区域模式!");
        else
            QMessageBox::information(nullptr, "提示", "已切换至普通模式!");
    });
}

void Algo_Cam_H_Form::InputValidator()
{
    QRegExp reg("^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
                "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
                "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
                "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$");
    ui->led_ipcip->setValidator(new QRegExpValidator(reg));
    ui->led_thresh->setValidator(new QRegExpValidator(QRegExp("^0\\.[1-9]{1,2}$")));
    ui->led_ipcuser->setValidator(new QRegExpValidator(QRegExp("^[A-Za-z0-9]+$")));
    ui->led_ipcpsw->setValidator(new QRegExpValidator(QRegExp("^[A-Za-z0-9]+$")));
}

void Algo_Cam_H_Form::on_btn_getparam_1_clicked()
{
    QJsonObject body;
    body.insert("chnId", m_chnid);
    Singleton::Instance()->onSendParam("GetAlgoParam", body);
}

void Algo_Cam_H_Form::on_btn_setparam_1_clicked()
{
    QJsonObject body;
    body.insert("chnId", ui->led_chnid->text());
    body.insert("enable", int(ui->sb_enable->getChecked()));
    body.insert("thresh", ui->led_thresh->text().toFloat());
    body.insert("url", ui->led_url->text());
    QStringList m_type = this->type();
    body.insert("eventType",  m_type.at(0));
    body.insert("alarmType", m_type.at(1));
    body.insert("alarmGrade", ui->cbx_alarmgrade->currentIndex());
    body.insert("ipcIp", ui->led_ipcip->text());
    body.insert("ipcUser", ui->led_ipcuser->text());
    body.insert("ipcPassword", ui->led_ipcpsw->text());
    Singleton::Instance()->onSendParam("SetAlgoParam", body);
}

void  Algo_Cam_H_Form::onGetAlgoParam(QJsonObject obj)
{
    ui->sb_enable->setChecked(obj.value("enable").toInt());
    ui->led_thresh->setText(QString::number(obj.value("thresh").toDouble(), 'f', 10));
    ui->led_url->setText(obj.value("url").toString());
    QString eventType = obj.value("eventType").toString();
    if(eventType == "PD_000")   ui->cbx_eventtype->setCurrentIndex(0);
    else if(eventType == "ML_000")   ui->cbx_eventtype->setCurrentIndex(1);
    if(obj.value("alarmType").toString() != "")
    {
        ui->cbx_alarmtype->setCurrentIndex(obj.value("alarmType").toString().split("_").at(1).toInt() - 1);
    }
    ui->cbx_alarmgrade->setCurrentIndex(obj.value("alarmGrade").toInt());
    ui->led_ipcip->setText(obj.value("ipcIp").toString());
    ui->led_ipcuser->setText(obj.value("ipcUser").toString());
    ui->led_ipcpsw->setText(obj.value("ipcPassword").toString());
}

void Algo_Cam_H_Form::on_btn_getparam_cam_f_baojing_clicked()
{
    QJsonObject body;
    body.insert("chnId", m_chnid);
    Singleton::Instance()->onSendParam("GetAlarmOut", body);
}



void Algo_Cam_H_Form::on_btn_setparam_cam_f_baojing_clicked()
{
    QJsonArray array;
    QJsonObject body_1;
    body_1.insert("alarmId", 1);
    body_1.insert("enable", int(ui->sb_dakuai->getChecked()));
    body_1.insert("io", ui->cbx_out_dakuai->currentIndex() + 1);
    array.append(body_1);
    QJsonObject body_2;
    body_2.insert("alarmId", 2);
    body_2.insert("enable", int(ui->sb_maogan->getChecked()));
    body_2.insert("io", ui->cbx_out_maogan->currentIndex() + 1);
    array.append(body_2);
    QJsonObject body_3;
    body_3.insert("alarmId", 3);
    body_3.insert("enable", int(ui->sb_shuimei->getChecked()));
    body_3.insert("io", ui->cbx_out_shuimei->currentIndex() + 1);
    array.append(body_3);
    QJsonObject body_4;
    body_4.insert("alarmId", 4);
    body_4.insert("enable", int(ui->sb_yiwu->getChecked()));
    body_4.insert("io", ui->cbx_out_yiwu->currentIndex() + 1);
    array.append(body_4);
    QJsonObject body;
    body.insert("chnId", m_chnid);
    body.insert("alarmOut", array);
    Singleton::Instance()->onSendParam("SetAlarmOut", body);
}

void  Algo_Cam_H_Form::onGetAlarmOut(QJsonObject obj)
{
    if(obj.value("alarmOut").isArray())
    {
        QJsonArray alarmOutArr = obj.value("alarmOut").toArray();
        for (int i = 0; i < alarmOutArr.count(); i++)
        {
            QJsonObject alarmOutObj = alarmOutArr.at(i).toObject();
            switch (alarmOutObj.value("alarmId").toInt())
            {
                case 1:
                    ui->sb_dakuai->setChecked(alarmOutObj.value("enable").toInt());
                    ui->cbx_out_dakuai->setCurrentIndex(alarmOutObj.value("io").toInt() - 1);
                    break;
                case 2:
                    ui->sb_maogan->setChecked(alarmOutObj.value("enable").toInt());
                    ui->cbx_out_maogan->setCurrentIndex(alarmOutObj.value("io").toInt() - 1);
                    break;
                case 3:
                    ui->sb_shuimei->setChecked(alarmOutObj.value("enable").toInt());
                    ui->cbx_out_shuimei->setCurrentIndex(alarmOutObj.value("io").toInt() - 1);
                    break;
                case 4:
                    ui->sb_yiwu->setChecked(alarmOutObj.value("enable").toInt());
                    ui->cbx_out_yiwu->setCurrentIndex(alarmOutObj.value("io").toInt() - 1);
                    break;
                default:
                    break;
            }
        }
    }
}
void Algo_Cam_H_Form::on_cbx_eventtype_currentIndexChanged(int eventtype)
{
    QStringList list_alarm ;
    switch (eventtype)
    {
        case 0:
            list_alarm << "异物识别" << "锚杆识别" << "大块识别" << "水煤识别" ;
            break;
        case 1:
            list_alarm << "皮带调速" << "煤量统计";
            break;
        default:
            break;
    }
    ui->cbx_alarmtype->clear();
    ui->cbx_alarmtype->addItems(list_alarm);
}

QStringList Algo_Cam_H_Form::type()
{
    QStringList type;
    QString eventtype;
    QString alarmtype;
    switch (ui->cbx_eventtype->currentIndex())
    {
        case 0:
            eventtype = "PD_000";
            alarmtype = QString("PD_%1").arg(ui->cbx_alarmtype->currentIndex() + 1, 3, 10, QChar('0'));
            break;
        case 1:
            eventtype = "ML_000";
            alarmtype = QString("ML_%1").arg(ui->cbx_alarmtype->currentIndex() + 1, 3, 10, QChar('0'));
            break;
        default:
            break;
    }
    type.append(eventtype);
    type.append(alarmtype);
    return  type;
}

void Algo_Cam_H_Form::on_btn_add_info_cam_f_clicked()
{
#if 0
    if(ui->widget_right->rects().count() > 0)
    {
        QList<DrawForm::DrawRect> rects = ui->widget_right->rects();
        for(int i = 0; i < rects.count(); ++i)
        {
            if(rects.at(i).selected)
            {
                rects[i].desc = QString("%1 %2").arg(ui->led_dakuaixinxi_cam_f->text()).arg(tr("grade") + QString::number(ui->cbx_dakuaidengji_cam_f->currentIndex() + 1));
                ui->widget_right->setRects(rects);
                ui->widget_right->update();
            }
        }
    }

#endif
    emit  sigaddinfo(QString("%1 %2").arg(ui->led_dakuaixinxi_cam_f->text()).arg(tr("grade") + QString::number(ui->cbx_dakuaidengji_cam_f->currentIndex())));
}

void Algo_Cam_H_Form::on_btn_getparam_cam_f_belttear_clicked()
{
    QJsonObject body;
    body.insert("chnId", m_chnid);
    Singleton::Instance()->onSendParam("GetBeltTearParam", body);
}

void Algo_Cam_H_Form::on_btn_setparam_cam_f_belttear_clicked()
{
    QJsonObject body;
    body.insert("chnId", m_chnid);
    QJsonObject body1;
    body1.insert("x", ui->led_x->text().toFloat());
    body1.insert("y", ui->led_y->text().toFloat());
    body1.insert("w", ui->led_w->text().toFloat());
    body1.insert("h", ui->led_h->text().toFloat());
    body.insert("zone", body1);
    Singleton::Instance()->onSendParam("SetBeltTearParam", body);
}



void Algo_Cam_H_Form::on_btn_getparam_cam_f_fromdraw_clicked()
{
    emit sigGetBeltTearZone();
}

void Algo_Cam_H_Form::ondrawBeltZone(QJsonObject body)

{
    ui->led_x->setText(QString::number(body.value("x").toDouble(), 'f', 10));
    ui->led_y->setText(QString::number(body.value("y").toDouble(), 'f', 10));
    ui->led_w->setText(QString::number(body.value("w").toDouble(), 'f', 10));
    ui->led_h->setText(QString::number(body.value("h").toDouble(), 'f', 10));
}

void Algo_Cam_H_Form::on_btn_setparam_cam_f_set_relay_clicked()
{
    QJsonObject body;
    body.insert("chnId", m_chnid);
    body.insert("enable", int(ui->sb_enable_2->getChecked()));
    body.insert("relay1", int(ui->sb_relay1->getChecked()));
    body.insert("relay2", int(ui->sb_relay2->getChecked()));
    body.insert("relay3", int(ui->sb_relay3->getChecked()));
    body.insert("relay4", int(ui->sb_relay4->getChecked()));
    body.insert("laser", int(ui->sb_laser->getChecked()));
    Singleton::Instance()->onSendParam("TestRelay", body);

}


void Algo_Cam_H_Form::on_btn_SetBrokenChainParam_clicked()
{
    QJsonObject body;
    body.insert("chnId", m_chnid);
    body.insert("BrokenAngle", ui->led_BrokenAngle->text().toFloat());
    body.insert("ObliqueAngle", ui->led_ObliqueAngle->text().toFloat());
    Singleton::Instance()->onSendParam("SetBrokenChainParam", body);
}
